<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/31 0031
 * Time: 下午 5:55
 */
namespace app\admin\controller;

class Communitydynamics extends Base{

    protected $isIframe = false;

    public function _initialize()
    {
        $this->exceptAction = [''];

        parent::_initialize();

        $this->defaultWhere = [
            // 'villageId' => $this->admin->villageId,
            'isDelete' => 2
        ];
        $this->searchFields = [
            'name' => [
                'label'     => '内容',
                'field'     => 'content',
                'type'      => 'text',
                'disabled'  => false,
                'condition' => 'like'

            ],
        ];
    }

    public function index(){
        $request = $this->request;
        $param = $request->param();
        // Reset filter
        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }
        if($request->isAjax()){
            $model = model($this->model);

            // 每页起始条数
            $start = $param['start'];
            // 每页显示条数
            $length = $param['length'];
            // 排序条件
            $columns = $param['order'][0]['column'];
            $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];
            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                $where = array_merge($this->defaultWhere, $where);
            }
            $list = $model->where($where)->limit($start, $length)->order($order)->select();
            foreach ($list as &$v){
                $user = \app\admin\model\User::get($v['createUser']);
                $v['user'] = $user;
            }
            $count = $model->where($where)->count();

            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count,
            ];

            return json($result);
        }
        return $this->fetch('list',[
            'pageSize' => ['url' => fullUrl($request->path())],
            'searchFields' => $this->searchFields,
            'param' => $request->param()]);
    }
}